package org.easyway.tiles;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import org.easyway.utils.Utility;

public class TileMap {

	public static ArrayList<TileMap> tileMapList = new ArrayList<TileMap>(5);

	public static TileMap getTileMap(String name) {
		for (TileMap tm : tileMapList) {
			if (tm.name.equals(name))
				return tm;
		}
		return null;
	}

	volatile ArrayList<TileManager> tileLayers;

	/**
	 * the number of tiles in width and height
	 */
	int width, height;

	/**
	 * the size of the tiles
	 */
	int tileWidth, tileHeight;

	/**
	 * the name of the TileMap
	 */
	public String name;

	public TileMap(int width, int height, int tileWidth, int tileHeight,
			String name, int nLayers) {
		tileLayers = new ArrayList<TileManager>(nLayers);
		this.width = width;
		this.height = height;
		this.tileHeight = tileHeight;
		this.tileWidth = tileWidth;
		this.name = name;
		tileMapList.add(this);
	}

	public void removeTileManager(TileManager tileManager) {
		tileLayers.remove(tileManager);
	}

	public void removeTileManager(int index) {
		tileLayers.remove(index);
	}

	public void addTileManager(TileManager tileManager) {
		if (tileManager.getNumX() == width && tileManager.getNumY() == height
				&& tileManager.getTileWidth() == tileWidth
				&& tileManager.getTileHeight() == tileHeight) {
			tileLayers.add(tileManager);

		} else {
			Utility
					.error(
							"the TileManager that you're adding to the TileMap isn't correct",
							"TileMap.addTileManager()", new Exception(
									"TileManager isn't correct"));
		}

	}
	
	public TileManager getTileManager(int index) {
		return tileLayers.get(index);
	}

	public static TileMap readFromFile(String file) {

		ObjectInputStream in = Utility.getLocalFile(file);
		if (in == null)
			return null;

		try {
			// read the version (at this time it's only 1)
			/* int version = */in.readInt();

			int width = in.readInt();
			int height = in.readInt();
			int tileWidth = in.readInt();
			int tileHeight = in.readInt();
			String name = (String) in.readObject();
			System.out.println("NAME "+name);

			int size = in.readInt();
			TileMap tileMap = new TileMap(width, height, tileWidth, tileHeight,
					name, size);

			TileManager tileManager;
			for (int i = 0; i < size; ++i) {
				tileManager = new TileManager(file + "_layer" + i,true,0,null);
				tileMap.addTileManager(tileManager);
			}
			in.close();
			return tileMap;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void writeOnFile(String fileName) {
		if (tileLayers == null || tileLayers.size() == 0) {
			Utility.error("tileMap is empty; What I should Write??",
					new Exception("tileMap is Empty"));
			return;
		}
		try {
			FileOutputStream fout = new FileOutputStream(fileName);
			ObjectOutputStream out = new ObjectOutputStream(fout);
			out.writeInt(1); // version
			// fileds
			out.writeInt(width);
			out.writeInt(height);
			out.writeInt(tileWidth);
			out.writeInt(tileHeight);
			out.writeObject(name);
			// tileLayers:
			// surely tileLayers exists and is not empty
			out.writeInt(tileLayers.size());
			for (int i = 0; i < tileLayers.size(); ++i) {
				tileLayers.get(i).writeOnFile(fileName + "_layer" + i);
			}

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
